package com.mucfc.crm;

import com.mucfc.ams.facade.IDrawAwardFacade;
import com.mucfc.crm.common.utils.FileUtil;
import com.mucfc.crm.common.utils.SftpClientApp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 优惠券接口测试
 * <p/>
 * Created by HuQingmiao on 2016/3/4.
 */
public class MucfcCouponClient {
    
    private static Logger log = LoggerFactory.getLogger(MucfcCouponClient.class);

    private static ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[]{"moa-dubbo-consumer.xml"});


    /**
     * CRM调用优惠券系统的派券接口
     *
     * @param activityId   优惠券系统产生的活动码
     * @param mySysCode    本方系统码：CRM
     * @param custBaseName 客群活动名称或某个主键，优惠券系统根据这个参数判断是否属重复调用；长度不能超过20位。
     * @param custNameList 客群名单，其中每项数据的格式为：手机号码|用户号|客户号|姓名|
     */
    public void callCouponInf(Long activityId, String mySysCode, String custBaseName, List<String> custNameList) throws Exception {

        // 从配置环境获取临时目录
        final String tmpDircName = "/app/tmp";

        File tmpDirc = new File(tmpDircName);
        if (!tmpDirc.exists()) {
            tmpDirc.mkdirs();
        }

        List<File> fileList = new ArrayList<File>(1);
        File namesFile = new File(tmpDirc, custBaseName + ".DAT");
        fileList.add(namesFile);


        // 生成客户名单文件
        log.info(">>生成客户名单文件");
        FileUtil.writeOut(namesFile, false, custNameList);


        // 上传文件到FTP
        log.info(">>上传文件到FTP");
        SftpClientApp ftpApp = new SftpClientApp();
        ftpApp.upload(null, fileList);
        namesFile.delete();


        // 调用派券dubbo接口
        log.info(">>调用派券dubbo接口,BEGIN...");
        try {
            // custBaseName 不能超过20位
            List<String> filenames = new ArrayList<String>(fileList.size());
            for (File file : fileList) {
                filenames.add(file.getName());
            }
            IDrawAwardFacade drawAwardService = (IDrawAwardFacade) context.getBean("drawAwardService");
            drawAwardService.batchSendCoupons(activityId, mySysCode, custBaseName, filenames);

            log.info(">>调用派券dubbo接口, END!");

        } catch (com.lz.lsf.exception.BusinessException e) {
            //如果错误码为：BAMSA012，表示优惠券系统上没有这个活动码
            log.info("ERROR_CODE: " + e.getCode());
            log.info("ERROR_MSG:  " + e.getMessage());
            throw e;
        }
    }


    public static void main(String[] args) {

        //Long actitityId =  new Long(args[0]);
        Long actitityId =2015091600000001L;

        String sysCode = "CRM";

        //String custBaseName = args[1];//
        String custBaseName = "10001003";

        List<String> custNameList = new ArrayList<String>();
        custNameList.add("15291469687|110000003369172|||");
        custNameList.add("13188457889|110000003471309|810001892906|闫泓旭|");

        try {
            log.info("BEGIN");

            MucfcCouponClient client = new MucfcCouponClient();
            client.callCouponInf(actitityId, sysCode, custBaseName, custNameList);

            log.info("END");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}